package util.sql;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 存在的问题，JSON中未对字符串进行转义处理
 * @author Administrator
 *
 */
public class JsonMap extends HashMap<String, Object>{
	private static final long serialVersionUID = 3885719308909513042L;

	private final String JSON_STRING = "-";
	
	public String getJsonStr(){
			StringBuilder strs = new StringBuilder();
			strs.append('{');
			for(Map.Entry<String, ?> entry : this.entrySet()){
				String key = entry.getKey();
				if(key.startsWith(JSON_STRING)){	
					strs.append('"');
					strs.append(key.substring(1));
					strs.append('"');
					strs.append(':');
					strs.append(entry.getValue());
				}else{
					strs.append('"');
					strs.append(entry.getKey());
					strs.append('"');
					strs.append(':');
					strs.append('"');
					if(null!=entry.getValue()){
						strs.append(String.valueOf(entry.getValue()).replace("\"", "\\\""));
					}
					strs.append('"');
				}
				strs.append(",");
			}
			return strs.substring(0, strs.length()-1)+'}';
	}
	
	public void putJson(String key, JsonMap map){
		put(JSON_STRING+key, map.getJsonStr());
	}
	/**
	 * @param jsonStr  如{"key":"value","key":"value"}
	 */
	public void putJson(String key, String jsonStr){
		put(JSON_STRING+key, jsonStr);
	}
	/**
	 * @param jsonArray 如[{"key":"value"},{"key":"value"}]
	 */
	public void putJsonArrayFromString(String key, String jsonArray){
		put(JSON_STRING+key, jsonArray);
	}
	/**
	 * @param jsonStrArray 每一个String都是JsonStr如{"key":"value","key":"value"}
	 */
	public void putJsonArrayFromJsonStringArray(String key, String[] jsonStrArray){
		String value = getJsonArrayFromJsonStringArray(jsonStrArray);
		put(JSON_STRING+key, value);
	}
	/**
	 * @param jsonStrList 每一个String都是JsonStr如{"key":"value","key":"value"}
	 */
	public void putJsonArrayFromJsonStringList(String key, List<String> jsonStrList){
		String value = getJsonArrayFromJsonStringList(jsonStrList);
		put(JSON_STRING+key, value);
	}
	public void putJsonArrayFromJsonMapList(String key, List<JsonMap> mapList){
		String value = getJsonArrayFromJSONMapList(mapList);
		put(JSON_STRING+key, value);
	}
	public void putJsonArrayFromJsonMapArray(String key, JsonMap[] mapArray){
		String value = getJsonArrayFromJSONMapArray(mapArray);
		put(JSON_STRING, value);
	}
	/**
	 * 
	 * @param list 元素为基本类型，基本类型的封装类，String类型
	 */
	public void putJsonArrayFromObjectList(String key, List<Object> list){
		String value = getJsonArrayFromObjectList(list);
		put(JSON_STRING+key, value);
	}
	/**
	 * 
	 * @param array 元素为基本类型，基本类型的封装类，String类型
	 */
	public void putJsonArrayFromObjectArray(String key, Object[] array){
		String value = getJsonArrayFromObjectArray(array);
		put(JSON_STRING+key, value);
	}

	/**
	 * @param list 里面的元素直接toString()
	 * @return 空[] 非空["string1","string2","string3","string4"]
	 */
	private String getJsonArrayFromObjectList(List<Object> list){
		if(null==list || list.isEmpty()){
			return "[]";
		}else{
			StringBuilder sb = new StringBuilder();
			sb.append('[');
			for(Object o : list){
				sb.append('"');
				sb.append(o);
				sb.append('"');
				sb.append(',');
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append(']');
			return sb.toString();
		}
	}
	
	/**
	 * @param array 里面的元素直接toString()
	 * @return 空[] 非空["string1","string2","string3","string4"]
	 */
	private String getJsonArrayFromObjectArray(Object[] array){
		if(null==array || array.length==0){
			return "[]";
		}else{
			StringBuilder sb = new StringBuilder('[');
			for(Object s : array){
				sb.append('"');
				sb.append(s);
				sb.append('"');
				sb.append(',');
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append(']');
			return sb.toString();
		}
	}

	/**
	 * 
	 * @param jsonStrList 每一个String都是JsonString格式{"key":"value"}
	 * @return
	 */
	private String getJsonArrayFromJsonStringList(List<String> jsonStrList){
		if(null==jsonStrList || jsonStrList.isEmpty()){
			return "[]";
		}else{
			StringBuilder sb = new StringBuilder();
			sb.append('[');
			for(String s : jsonStrList){
				sb.append(s);
				sb.append(',');
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append(']');
			return sb.toString();
		}
	}
	/**
	 * 
	 * @param array 每一个String都是JsonString格式{"key":"value"}
	 * @return
	 */
	private String getJsonArrayFromJsonStringArray(String[] array){
		if(null==array || array.length==0){
			return "[]";
		}else{
			StringBuilder sb = new StringBuilder('[');
			for(String s : array){
				sb.append(s);
				sb.append(',');
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append(']');
			return sb.toString();
		}
	}
	
	private String getJsonArrayFromJSONMapList(List<JsonMap> list){
		if(null==list || list.isEmpty()){
			return "[]";
		}else{
			StringBuilder sb = new StringBuilder();
			sb.append('[');
			for(JsonMap map : list){
				sb.append(map.getJsonStr());
				sb.append(',');
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append(']');
			return sb.toString();
		}
	}
	
	private String getJsonArrayFromJSONMapArray(JsonMap[] array){
		if(null==array || array.length==0){
			return "[]";
		}else{
			StringBuilder sb = new StringBuilder();
			sb.append('[');
			for(JsonMap map : array){
				sb.append(map.getJsonStr());
				sb.append(',');
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append(']');
			return sb.toString();
		}
	}
}
